package com.crm.controller.purchase;

import com.alibaba.druid.util.StringUtils;
import com.crm.dao.purchaseDAO.PurchaseDAO;
import com.crm.exception.ServiceException;
import com.crm.pojo.Purchase;
import com.crm.pojo.Purchaseproductiondetail;
import com.crm.pojo.Purchasergdetail;
import com.crm.service.purchaseService.PurchaseService;
import com.crm.service.purchaseService.PurchasergService;
import com.crm.service.purchaseService.PurproductDetailsService;
import com.crm.vo.Result;
import org.hibernate.annotations.AttributeAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ProPurchasergController {
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private PurchasergService purchasergService;
    @Autowired
    private PurproductDetailsService purproductDetailsService;

    @GetMapping("pPurchasergM")
    public String toPurchaserg(){
        return "purchase/proPurchaserg";
    }

    //打开新建采购退货单表单
    @GetMapping("to-form-purrg-add")
    public String toFormPurrgadd(){
        return "purchase/newPurchaserg";
    }
    //模糊查询采购主题 采购完成的采购单
    @PostMapping("getAllPurchaseBymh")
    @ResponseBody
    public List<Purchase> getAllPurchaseBymh(String q){
        return purchaseService.findAllBythememh(q);
    }
    //根据采购id查询采购产品明细
    @PostMapping("findpurprodetailBypurid")
    @ResponseBody
    public List<Purchaseproductiondetail> findPurprodetailBypurid(Long q){
        return purproductDetailsService.findAllppdByPurid(q);
    }
    //提交表单
    @PostMapping("newPurchaserg-form-sub")
    @ResponseBody
    public Result newPurchasergForm(Purchasergdetail purchasergdetail){
        try{
            Purchaseproductiondetail purchaseproductiondetail = purproductDetailsService.findOne(purchasergdetail.getPpurdid());
            if(purchaseproductiondetail.getPpcost()==null){
                purchaseproductiondetail.setPpcost(0D);
            }
            if(purchaseproductiondetail.getPpcount()==null){
                purchaseproductiondetail.setPpcount(0L);
            }
            purchasergdetail.setPrgmoney(purchaseproductiondetail.getPpcost());
            purchasergdetail.setPrgcount(purchaseproductiondetail.getPpcount());
            if(purchasergdetail.getPuridfk()!=null){
                String status = purchaseService.findOneById(purchasergdetail.getPuridfk()).getPurstatus();
                if(status.equals("采购完成-待入库")){
                    //删除对应采购单的采购商品明细(逻辑删除) 对于采购完成-待入库
                    purproductDetailsService.deleteOne(purchaseproductiondetail);
                    purchasergdetail.setPurchasergdetailsIsDel("退货完成");
                }else if(status.equals("采购完成-已入库")){
                    //设置对应采购单的采购商品明细状态为已退货 对于采购完成-已入库
                    purproductDetailsService.setrg(purchaseproductiondetail);
                    purchasergdetail.setPurchasergdetailsIsDel("退货完成-待出库");
                }
            }
            //添加采购退货明细
            purchasergService.save(purchasergdetail);
            return Result.SUCCESS;
        }catch(ServiceException e){
            e.printStackTrace();
            return new Result("添加失败！");
        }
    }
    //分页查询所有采购退货单
    @PostMapping("allPurrgPaged")
    @ResponseBody
    public Map allProPaged(int page, int rows) {
        Long total = purchasergService.queryCount();
        List<Purchasergdetail> pro = purchasergService.allPurchasergPaged(page,rows);
        HashMap map = new HashMap<>();
        map.put("total", total);
        map.put("rows", pro);
        return map;
    }

    //查询所有的采购退货单通过采购主题和供应商名称模糊查询
    @PostMapping("getAllPurrgBymh")
    @ResponseBody
    public List<Purchasergdetail> getAllPurrgBypurthemeAndClientname(String q,String b){
        List<Purchasergdetail> plist =null;
        if(!StringUtils.isEmpty(q)&&!StringUtils.isEmpty(b)){
            if("rg1".equals(b)){
                plist =  purchasergService.getAllPurrgByMh(q);
            }else if("rg2".equals(b)){
                plist = purchasergService.getAllPurrgByMh1(q);
            }else{
                plist = purchasergService.getAllPurrgByMh2(q);
            }
        }
        return plist;
    }
}
